package org.arestaurant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class Tables extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!ConnectionServices.checkHttpConnection()) {
            Intent intent = new Intent(this, Setting.class);
            startActivity(intent);
        } else {
            setContentView(R.layout.tables);
            JSONArray tabletList = TableServices.getTableList();
            LinearLayout tablesLayout = (LinearLayout)findViewById(R.layout.tables);
            try {
                for (int index = 0; index < tabletList.length(); index++) {
                    final JSONObject table = tabletList.getJSONObject(index);
                    final Button tableBtn = new Button(this);
                    tableBtn.setText(table.getString("workEffortName"));
                    tableBtn.setPadding(5, 5, 5, 5);
                    String tableSelected = Table.TABLE_ID_SELECTED.toLowerCase();
                    String tableNew = table.getString("workEffortId").toLowerCase();
                    if (tableSelected.equals(tableNew)) {
                        tableBtn.setTextColor(Color.rgb(255, 0, 0));
                    } else {
                        tableBtn.setTextColor(Color.rgb(100, 100, 100));
                    }
                    tableBtn.setOnLongClickListener(new OnLongClickListener() {
                        public boolean onLongClick(View arg0) {
                            try {
                                Table.TABLE_ID_SELECTED = table.getString("workEffortId");
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            openTablePopup(tableBtn);
                            return false;
                        }
                    });
                    tablesLayout.addView(tableBtn);
                }
                ConnectionServices.disconnect();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private void openTablePopup(Button tableBtn) {
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.table_popup, null);
        final PopupWindow pw = new PopupWindow(layout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
        Button detailBtn = (Button)layout.findViewById(R.id.tableDetail);
        detailBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                // close the popup
                pw.dismiss();
                openTable();
            }
        });
        Button selectTableBtn = (Button)layout.findViewById(R.id.selectTable);
        selectTableBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                // close the popup
                pw.dismiss();
                selectTable();
            }
        });
        pw.showAtLocation(tableBtn, Gravity.CENTER, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }
    private void openTable() {
        Intent intent = new Intent(this, Table.class);
        startActivity(intent);
    }
    private void selectTable() {
        recreate();
    }
}